function ajax_filteruser(form) {
	var username = form.username.value;
	var password = form.pwd.value;
	if(username==''){
		$("#loginmsg").text("用户名不能为空!");
		return false;
	}
	if(password==''){
		$("#loginmsg").text("密码不能为空!");
		return false;
	}
	$.ajax({
        "type": "GET",
        "url": "/login.html?username=" + username + "&password=" + password,
        "data": "",
        "dataType": "json",
        "success": function(result) {
            if(result.result=='ok'){
            	window._loginbox.hide();
            	setuserinfo(result.username,result.usergrade);
            }else{
            	$("#loginmsg").text("用户名或密码错误!");
            }
        }
    });
}

function setuserinfo(username,usergrade){
	//隐藏登录框
	$("#topLoginBox").css('display','none');	
	//显示用户名
	$("#userinfor").css('display','');
	
	$("#message_nologin").css('display','none');
	$("#message_login").css('display','');
	
	$("#username").text(username);
	$("#usergrade").html("L"+usergrade+"<div class=\"gradeRight\"></div>");
}

$(function(){
	$.ajax({
        "type": "GET",
        "url": "/getuser.html",
        "data": "",
        "dataType": "json",
        "success": function(result) {
            if(result.result=='ok'){
            	setuserinfo(result.username,result.usergrade);
            }
        }
    });
});	

function stoppropagation(a) {
    $.browser.msie ? event.cancelBubble = !0 : (a.stopPropagation(), a.preventDefault());
}

function ajax_login() {
    var html = "";
    html += '    <div class="loginBg"></div>', 
    html += '    <form class="loginWrap"  id="ajaxLoginForm">', 
    html += '        <div class="loginHeader">', 
    html += '            <span class="loginTopTitle">登录</span>', 
    html += '            <a href="#" class="closeCorner">关闭</a>', 
    html += '            <div class="loginReg">没有账号？<a href="/register.html">快速注册</a></div>', 
    html += '            <div class="clear"></div>', 
    html += "        </div>", 
    html += '        <div id="loginMsg" >', 
    html += "        </div>", 
    html += '        <div class="perDataItem">', 
    html += '            <label for="userLogin">用户名：</label>', 
    html += '            <input type="text" name="username" id="userLogin" class="inputData" tabindex="100"/>', 
    html += "        </div>", 
    html += '        <div class="perDataItem">', 
    html += '            <label for="userPass">密码：</label>', 
    html += '            <input type="password" name="pwd" id="userPass" class="inputData" tabindex="101"/>', 
    html += "        </div>",
    html += '        <div class="loginBtnBox">', 
    html += '            <input type="button" id="ajax-submit" name="ajax-submit" class="loginBtn" value="登&nbsp;&nbsp;录" onclick="ajax_filteruser(this.form);" tabindex="105"/>', 
    html += '            <span id="loginmsg"></span>', 
    html += "        </div>", 
    html += '        <div class="dataItemBot">       ', 
    html += '            <a rel="nofollow" title="腾讯微博" href="' + smzdm_site_domain + 'wp-content/plugins/clogin/login.php?go=tencent&amp;redirect_to=/">', 
    html += '                <img src="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_tencent.png" original="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_tencent.png">', 
    html += "            </a>", 
    html += '            <a rel="nofollow" title="新浪微博" href="' + smzdm_site_domain + 'wp-content/plugins/clogin/login.php?go=sina&amp;redirect_to=/">', 
    html += '                <img src="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_sina.png" original="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_sina.png">', 
    html += "            </a>", 
    html += '            <a rel="nofollow" title="腾讯QQ" href="' + smzdm_site_domain + 'wp-content/plugins/clogin/login.php?go=qzone&amp;redirect_to=/">', 
    html += '                <img src="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_qzone.png" original="' + smzdm_site_domain + 'wp-content/plugins/clogin/images/btn_qzone.png">', 
    html += "            </a>",
    html += '            <span class="wordTitle">您可以用合作网站帐号登录:</span>', 
	html += "        </div>", 
	html += "    </form>", 
	
		window._loginbox || (window._loginbox = $('<div class="loginBox"></div>'), 
		$(document.body).append(window._loginbox), window._loginbox.html(html), $(document).on("keydown", function(b) {        
		b.keyCode == 13 && (stoppropagation(b), _fml = $("#ajaxLoginForm"), ajax_filteruser(_fml[0]));
    })), window._loginbox.show(), $("#userLogin").focus(), jQuery("html, body").animate({
        "scrollTop": 0
    }, 0), $(".closeCorner").off("click").on("click", function() {
        window._loginbox.hide();
    });
}


function checkusername(){
	var username = $("#regusername").val();
	if(username==null || username==''){
		$("#usernamemsg").text("用户名不能为空！");
		return false;
	}
	$.ajax({
        "type": "GET",
        "url": "/checkusername.html?username=" + username,
        "data": "",
        "dataType": "json",
        "success": function(result) {
        	if(result.result=='no'){
        		$("#usernamemsg").text("该名称太火爆已经被抢注！");
        	}else{
        		$("#usernamemsg").text("用户名可用！");
        	}
        }
    });
	return true;
}

function checkemail(){
	var email = $("#user_email").val();
	if(email==null || email==''){
		$("#emailmsg").text("邮箱不能为空！");
		return false;
	}
	var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(!emailreg.test(email)){
		$("#emailmsg").text("邮箱格式不正确！");
		return false;
	}
	$.ajax({
        "type": "GET",
        "url": "/checkemail.html?email=" + email,
        "data": "",
        "dataType": "json",
        "success": function(result) {
        	if(result.result=='no'){
        		$("#emailmsg").text("该邮箱已经注册过！");
        	}else{
        		$("#emailmsg").text("邮箱可用！");
        	}
        }
    });	
	return true;
}

function checkpassword1(){
	var user_pwd1=$("#user_pwd1").val();
	var user_pwd2=$("#user_pwd2").val();
	if(user_pwd1==''){
		$("#user_pwd1_msg").text("密码不能为空！");
		return false;
	}
	if(user_pwd1.length<6){
		$("#user_pwd1_msg").text("密码至少6位！");
		return false;
	}
	if(user_pwd2!='' && user_pwd1!=user_pwd2){
		$("#user_pwd1_msg").text("密码不相等！");
		return false;
	}
	$("#user_pwd1_msg").text("密码可用！");
	return true;
}

function checkpassword2(){
	var user_pwd1=$("#user_pwd1").val();
	var user_pwd2=$("#user_pwd2").val();
	if(user_pwd2==''){
		$("#user_pwd2_msg").text("密码不能为空！");
		return false;
	}
	if(user_pwd1!=user_pwd2){
		$("#user_pwd2_msg").text("密码不相等！");
		return false;
	}
	if(user_pwd2.length<6){
		$("#user_pwd2_msg").text("密码至少6位！");
		return false;
	}
	checkpassword1()
	$("#user_pwd2_msg").text("密码可用！");
	return true;
}

function checkcheckcode(){
	var checkcode = $("#checkcode").val();
	if(checkcode==''){
		$("#checkcodemsg").text("验证码不能为空！");
		return false;
	}
	return true;
}

function logout(){
	location.href="http://www.smzdmya.com/logout.html";
}

function submitreg(){
	if(checkusername()&&checkemail()&&checkpassword1()&&checkpassword2()&&checkcheckcode()){
		$("#registerform").submit();
	}
}
